package com.xzc.drools.analysis;

import org.drools.impact.analysis.graph.Graph;
import org.drools.impact.analysis.graph.ModelToGraphConverter;
import org.drools.impact.analysis.graph.TextReporter;
import org.drools.impact.analysis.graph.graphviz.GraphImageGenerator;
import org.drools.impact.analysis.model.AnalysisModel;
import org.drools.impact.analysis.parser.internal.ImpactAnalysisKieModule;
import org.drools.impact.analysis.parser.internal.ImpactAnalysisProject;
import org.junit.Test;
import org.kie.api.KieServices;
import org.kie.api.builder.KieBuilder;
import org.kie.api.builder.KieFileSystem;
import org.kie.api.builder.Message;
import org.kie.api.builder.model.KieBaseModel;
import org.kie.api.builder.model.KieModuleModel;
import org.kie.api.builder.model.KieSessionModel;
import org.kie.api.conf.EqualityBehaviorOption;
import org.kie.api.conf.EventProcessingOption;
import org.kie.api.runtime.conf.ClockTypeOption;

import java.io.FileInputStream;
import java.io.FileNotFoundException;

import static org.junit.Assert.assertEquals;

public class A1 {

    @Test
    public void test3() throws FileNotFoundException {
        KieServices kieServices = KieServices.Factory.get();
        KieModuleModel kieModuleModel = kieServices.newKieModuleModel();
        KieBaseModel kieBaseModel1 = kieModuleModel.newKieBaseModel("KBase1")
                .setDefault(true)
                .setEqualsBehavior(EqualityBehaviorOption.EQUALITY)
                .setEventProcessingMode(EventProcessingOption.STREAM);
        KieSessionModel kieSessionModel = kieBaseModel1.newKieSessionModel("KSession1")
                .setDefault(true)
                .setType(KieSessionModel.KieSessionType.STATEFUL)
                .setClockType(ClockTypeOption.get("realtime"));
        KieFileSystem kfs = kieServices.newKieFileSystem();
        kfs.write("src/main/resources/rules/book/discount/bookDiscount.drl"
                , kieServices.getResources().newInputStreamResource(new FileInputStream("src/main/resources/rules/book/discount/bookDiscount.drl")));
        kfs.write("src/main/resources/rules/comparisonOperator/comparisonOperator.drl"
                , kieServices.getResources().newInputStreamResource(new FileInputStream("src/main/resources/rules/comparisonOperator/comparisonOperator.drl")));
        KieBuilder kieBuilder = kieServices.newKieBuilder(kfs).buildAll(ImpactAnalysisProject.class);
        assertEquals(0, kieBuilder.getResults().getMessages(Message.Level.ERROR).size());
//        KieContainer kieContainer = kieServices.newKieContainer(kieServices.getRepository().getDefaultReleaseId());
        ImpactAnalysisKieModule analysisKieModule = (ImpactAnalysisKieModule) kieBuilder.getKieModule();
        AnalysisModel analysisModel = analysisKieModule.getAnalysisModel();
        ModelToGraphConverter converter = new ModelToGraphConverter();
        Graph graph = converter.toGraph(analysisModel);
//        ImpactAnalysisHelper impactFilter = new ImpactAnalysisHelper();
//        Graph impactedSubGraph = impactFilter.filterImpactedNodes(graph, "rule_comparison_contains");
        GraphImageGenerator generator = new GraphImageGenerator("example-impacted-sub-graph");
        generator.generateSvg(graph);
        String hierarchyText = TextReporter.toHierarchyText(graph);
        System.out.println(hierarchyText);
    }
}
